{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# Requires running FixingFil4_WCS to measure the relative shift in RA and Dec between Flights 684 and 685\n",
    "# Need to modify extension for each file, see reprojection library"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Fixing the WCS information for all the files for Flight 684"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from astropy.io import fits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Files to be modified:\n",
      "\n",
      "F0684_HA_POL_08018666_HAWEHWPE_SMP_001-012.fits\n",
      "F0684_HA_POL_08018667_HAWEHWPE_SMP_029-036.fits\n",
      "F0684_HA_POL_08018692_HAWEHWPE_SMP_013-024.fits\n",
      "F0684_HA_POL_08018692_HAWEHWPE_SMP_049-060.fits\n",
      "F0684_HA_POL_08018692_HAWEHWPE_SMP_061-072.fits\n",
      "F0684_HA_POL_08018693_HAWEHWPE_SMP_037-048.fits\n",
      "\n",
      "Number of files to fix: 6\n"
     ]
    }
   ],
   "source": [
    "# Fixing the WCS information for all the files for Flight 684\n",
    "\n",
    "print('Files to be modified:')\n",
    "print('')\n",
    "\n",
    "# Reading all the files using AOR 92 for flight 684\n",
    "RefDir = 'Original/'\n",
    "OutDir = 'Fixed/'\n",
    "RefFiles = []\n",
    "for entry in os.scandir(RefDir):\n",
    "    if entry.is_file() and entry.name.endswith('.fits'):\n",
    "        print(entry.name)\n",
    "        RefFiles.append(entry.name)\n",
    "\n",
    "# Number of files to fix\n",
    "RefSize = len(RefFiles)\n",
    "print('')\n",
    "print('Number of files to fix: '+str(RefSize))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Function definitions\n",
    "\n",
    "# Function to hack a header\n",
    "def fix_CRVAL(header, DeltaRA, DeltaDec):\n",
    "\t# Convert value of the central pixel\n",
    "\tcrval1 = header['CRVAL1']\n",
    "\tcrval2 = header['CRVAL2']\n",
    "\theader['CRVAL1'] = crval1 + DeltaRA\n",
    "\theader['CRVAL2'] = crval2 + DeltaDec\n",
    "\n",
    "\treturn\n",
    "\n",
    "# Function to hack all the header of all the extensions of a LEVEL 2 HAWC+ FITS file\n",
    "def fix_header_wcs(headersfile, DeltaRA, DeltaDec):\n",
    "\t# HAWC+ files are expected to have 16 extensions\n",
    "\tfor i in range(0,24):\n",
    "\t\tfix_CRVAL(headersfile[i].header, DeltaRA, DeltaDec)\t\n",
    "\treturn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Measured difference in RA and Dec\n",
    "DeltaRA = -0.049397\n",
    "DeltaDec = 0.130828\n",
    "\n",
    "# Iterating on the list of files to fix\n",
    "for i in range(0,RefSize):\n",
    "    # Opening the fits file\n",
    "    FitsFile = fits.open(RefDir+RefFiles[i])\n",
    "    # Fixing the header using the correction in RA and Dec\n",
    "    fix_header_wcs(FitsFile, DeltaRA, DeltaDec)\n",
    "    # Saving the modified file\n",
    "    FitsFile.writeto(OutDir+RefFiles[i], overwrite='true')\n",
    "    # Closing the fits container\n",
    "    FitsFile.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "PoLiteWIP",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
